import { Destroyable, react, reactJson, Event, track, HasOwner, reactJsonWithUndefined } from "xbsj-xe2/dist-node/xe2-base-utils";
import { MyProjectManager } from "../MyProjectManager";
import { Child, TransformedChild } from '@/scripts/dataTypes/interface'
import { ObjResettingWithEvent, SceneObject, Viewer } from 'xbsj-xe2/dist-node/xe2-base-objects';
import { ESRtsFeatureEditing } from "../esobjs/ESRtsFeatureEditing";
import { getsceneObjNumfromSceneTree } from "../utils";
import { ElMessage } from "element-plus";
import { ESRtsEexturedPolygon } from "../esobjs/ESRtsEexturedPolygon";
import { ESRtsPolygon } from "../esobjs/ESRtsPolygon";
import { ESFeTreeEditing } from "../esobjs/ESFeTreeEditing";
import { ESCzmViewer } from "esobjs-xe2-plugin/dist-node/esobjs-xe2-plugin-main";

function isNumber(value: any): boolean {
    return typeof value === 'number' && !isNaN(value);
}
class PickResettting extends Destroyable {
    constructor(editTree: EditTree, owner: MyProjectManager) {
        super();
        const eSForest = owner.eSForestManager.eSForest
        const viewer = owner.activeViewer

        if (!viewer) return;
        const objShow = (obj: any, show: boolean) => {
            if (viewer instanceof ESCzmViewer) {
                // 隐藏旧的
                obj.show = show
            } else {
                // ue通过快速砍倒隐藏
                if (show) {
                    const treeParam = eSForest.trees.find(item => item.id == obj.treeId)
                    if (!treeParam) return;
                    eSForest.update([treeParam]);
                    return;
                }
                eSForest.remove([obj.treeId])
            }
        }
        this.dispose(viewer.clickEvent.don(async e => {

            if (!e.screenPosition) return;
            // 拾取
            const pick = await eSForest.pickTree(viewer, e.screenPosition)
            const obj = pick?.childPickedInfo?.childPickedInfo?.czmPickResult ?? pick?.childPickedInfo?.childPickedInfo?.uePickResult;
            console.log("pick", pick);
            if (!obj || (!obj.hasOwnProperty("_featureId") && !obj.hasOwnProperty("treeId")) || this.isDestroyed()) return;
            objShow(obj, false);
            let sceneObject: any
            const trees = owner.eSForestManager.eSForest.trees
            const index = obj.featureId ?? obj.treeId;
            const tree = typeof index == 'number' ? trees[index] : trees.find((tree: any) => tree.id == index)
            if (!tree) return;
            const { id, treeType, position, guanfu, xiongjing, zhixiagao, shugao } = tree;

            sceneObject = owner.createSceneObject(ESFeTreeEditing, id) as ESFeTreeEditing
            sceneObject.dispose(() => objShow(obj, true))

            sceneObject.position = position
            sceneObject.plotId = owner.eSForestManager.esForestPlotId
            sceneObject.time = owner.eSForestManager.esForestTime

            sceneObject.treeModelClassName = treeType
            sceneObject.shugao = shugao
            sceneObject.xiongjing = xiongjing
            sceneObject.zhixiagao = zhixiagao
            // @ts-ignore
            sceneObject.dongxiguanfu = guanfu[0]
            // @ts-ignore
            sceneObject.nanbeiguanfu = guanfu[1]

            const param = eSForest.formatData([tree], eSForest.treeTypesAll);
            sceneObject.scale = param[0].scale
            sceneObject.url = eSForest.treeTypesAll[param[0].treeTypeId].cesiumAssetServerPath
            const rotation = eSForest.treesParam.find(item => item.id == id)?.rotation;
            sceneObject.rotation = rotation ?? [0, 0, 0];

            sceneObject.editing = true

            const treeItem = owner.createdTreeItem(owner.forestSceneTreeWrapper)
            treeItem.sceneObject = sceneObject;
            const name = sceneObject.treeModelClassName
            const num = getsceneObjNumfromSceneTree(sceneObject.typeName, owner.forestSceneTreeWrapper);
            treeItem.name = `编辑_${name}(${num})`
            owner.forestSceneTreeWrapper?.uiTree.clearAllSelectedItems()
            treeItem.uiTreeObject.selected = true

            editTree.treeInfo = [
                {
                    key: "ID",
                    value: sceneObject.id,
                },
                {
                    key: "树种",
                    value: sceneObject.treeModelClassName,
                },
                {
                    key: "树高",
                    value: sceneObject.shugao,
                },
                {
                    key: "胸径",
                    value: sceneObject.xiongjing,
                },
                {
                    key: "枝下高",
                    value: sceneObject.zhixiagao,
                },
                {
                    key: "东西冠幅",
                    value: sceneObject.dongxiguanfu,
                },
                {
                    key: "南北冠幅",
                    value: sceneObject.nanbeiguanfu,
                },
            ]

            owner.forestPropSceneTree = {
                createOrEdit: 'edit',
                propType: "addTree",
                sceneObject: sceneObject,
            }



        }))
    }
}

export class EditTree extends HasOwner<MyProjectManager> {

    // 开启拾取
    private _enablePick = this.dv(react<boolean>(false));
    get enablePick() { return this._enablePick.value; }
    get enablePickChanged() { return this._enablePick.changed; }
    set enablePick(value: boolean) { this._enablePick.value = value; }

    // 树木信息
    private _treeInfo = this.dv(reactJson<any>([]));
    get treeInfo() { return this._treeInfo.value; }
    get treeInfoChanged() { return this._treeInfo.changed; }
    set treeInfo(value: any) { this._treeInfo.value = value; }

    // 拾取处理
    private _pickResettting = this.dv(new ObjResettingWithEvent(this.enablePickChanged, () => {
        if (!this.enablePick) return undefined;
        return new PickResettting(this, this.owner);
    }))
    get pickResettting() { return this._pickResettting; }

    init() {
        this.enablePick = true
    }
    clear() {
        this.enablePick = false
        this.treeInfo = []
    }

    constructor(owner: MyProjectManager) {
        super(owner);
    }
}







